﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using DaInterFace;
using DaInterFace.PDA;
using DaInterFace.PDA.P02;

namespace P02
{
    /// <summary>
    /// 获得所有质检订单信息
    /// </summary>
    public class A01 : IPDAChild
    {
        public void Code(IPDA handler, CodeResult CR, PDACodeUser user, String parameter)
        {
            //P01A10P P = (P01A10P)handler.XML.Deserialize(typeof(P01A10P), parameter);

            P02A01R R = new P02A01R();
            
            try
            {
                DataTable table = new DataTable();
                table.TableName = "Order";

                foreach (SetOfBook book in handler.Data.GetAccounts().Values)
                {
                    StringBuilder sql = new StringBuilder();
                    sql.AppendFormat("Select A.FID,A.FBillNo,B.FName SName,C.FName TName,{0} BaseId From KEW_PurchOrderQty A\r\n", book.FNumber);
                    sql.Append("Left Join t_Supplier B On A.FBase=B.FItemID \r\n");
                    sql.Append("Left Join KEW_PurchItem C On A.FBase4=C.FID \r\n");
                    sql.Append("Where FUser>0 AND IsNull(FBase6,0)>0 ORDER BY A.FBillNo");
                    Recordset rs = handler.Data.ExeProcedure(book.FNumber, sql.ToString());
                    if (rs.Enable)
                    {
                        table.Merge(rs.Data.Tables[0]);
                    }
                    else
                    {
                        CR.Result = EnumResult.Error;
                        CR.Message = "数据库错误";
                        break;
                    }
                }
                R.Table = table;

                CR.Content = handler.SerialApi.Serializer(typeof(P02A01R), R, CR.SerialType);

                CR.Message = String.Format("操作员{0}获得所有订单信息", user.UserName);

            }
            catch (Exception ex)
            {
                CR.Result = EnumResult.Error;
                CR.Message = ex.Message;
                handler.Message(MessageType.Error, String.Format("P02A01发生错误:{0}", ex.Message));
            }
        }
    }
}
